﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Collections; 
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Collections.Generic;
using Runda.CMS.Common.Utility;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using log4net;
using Runda.CMS.Common.Log;

namespace Runda.CMS.Portal.Mobile
{
    /// <summary>
    /// Summary description for PortalService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class MobilePortalService : System.Web.Services.WebService
    {

        [WebMethod(true)]
        public LoginServiceEntity MobileLogin(string Account, string Password)
        {
            ILog log = log4net.LogManager.GetLogger("myLogger");

            LoginServiceEntity ls = new LoginServiceEntity();

            List<SqlDbParameter> parms = new List<SqlDbParameter>();
            SqlDbParameter parm = new SqlDbParameter();
            parm.ColumnName = "Account";
            parm.ColumnType = DbType.String;
            parm.ParameterName = "@Account";
            parm.ParameterValue = Account;
            parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
            parms.Add(parm);

            parm = new SqlDbParameter();
            parm.ColumnName = "Password";
            parm.ColumnType = DbType.String;
            parm.ParameterName = "@Password";
            parm.ParameterValue = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Password, "MD5");
            parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
            parms.Add(parm);

            SysUserBll bll = new SysUserBll();
            SysUserEntity entity = bll.Get<SysUserEntity>(parms);
            if (entity != null)
            {
                ls.IsLogin = true;

                parms = new List<SqlDbParameter>();
                parm = new SqlDbParameter();
                parm.ColumnName = "UserID";
                parm.ColumnType = DbType.Guid;
                parm.ParameterName = "@UserID";
                parm.ParameterValue = entity.UserID;
                parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
                parms.Add(parm);
                entity.UserRoleRelationList = new SysUserRoleRelationBll().GetAll<SysUserRoleRelationEntity>(parms);

                Session[DeclareHelper.SessionConstant.CurrentUserID] = entity.UserID;
                Session[DeclareHelper.SessionConstant.CurrentUserEntity] = entity;

                log.Info(new LogContent(LogType.info, "UserLogin", entity.UserID.ToString(), "", Account + "登陆成功"));
            }
            else
            {
                ls.IsLogin = false;
                ls.MessageUI = "用户名或密码错误!";
                log.Info(new LogContent(LogType.info, "UserLogin",Guid.Empty.ToString(), "", "登陆失败"));
            }
            return ls;
        }
        [WebMethod(true)]
        public String MobileLogOut()
        {
            Session.Abandon();
            return "{outcome:true}";
        }

        [WebMethod(true)]
        public Hashtable  getCustomer(string nameStr)
        {
            string customers = "";
            List<CtrtCustomerEntity> customerList = new SystemBll().GetAll<CtrtCustomerEntity>(" and CustomerName like '%"+nameStr+"%'");
            foreach (CtrtCustomerEntity customer in customerList)
            {
                if (customers != "")
                {
                    customers += ";";
                }
                customers = customers + customer.CustomerID.ToString() + "|" + customer.CustomerName;
            }
            Hashtable ht = new Hashtable();
            ht.Add("outcome", customers); 
            return ht;
        }
    }
}
